<template>
  <div class="vlonFloatButton" v-if="oItem">
    <span class="title">{{ oItem.title }}</span>
    <div class="right">
      <el-radio-group
        v-model="oItem.value"
        v-if="oItem.ename == 'floatButtonEvent'"
        @change="floatBtnEventChange"
      >
        <el-radio v-for="o in oItem.options" :key="o.id" :label="o.id">{{
          o.name
        }}</el-radio>
      </el-radio-group>

      <div class="chooseLink" v-if="oItem.value == 'link'">
        <chooseLinkComponent
          data-title="悬浮按钮跳转链接"
          :item="{
            title: oItem.options[0].link.title,
            linkName: oItem.options[0].link.name,
            linkUrl: oItem.options[0].link.url,
          }"
          :id="id"
          :rightIndex="rightIndex"
          :catalogIndex="catalogIndex"
          @updateParent="updateParent"
        ></chooseLinkComponent>
      </div>
      <div class="chooseImage" v-if="oItem.value == 'qrcode'">
        <div class="chooseImageComponent" data-title="悬浮按钮弹出二维码图片">
          <span class="title">{{ item.options[1].qrcode.title }}</span>
          <div class="right">
            <div class="buttonWrapper">
              <div class="uploadBtn" v-if="!value" @click="openChooseImg">
                <i class="el-icon-plus"></i>
              </div>

              <i
                class="content"
                v-if="value && String(value).indexOf('icon-') > -1"
                :class="value"
              ></i>

              <img
                v-if="value && String(value).indexOf('icon-') == -1"
                :src="value"
                alt
              />

              <span v-if="value" class="repaceImg" @click="updateChooseImg"
                >替换</span
              >

              <span
                v-if="value"
                class="el-icon-error delImg"
                @click="delImg"
              ></span>
            </div>
          </div>
        </div>
      </div>

      <el-radio-group
        v-model="oItem.value"
        @change="floatButtonPositionChange"
        v-if="oItem.ename == 'floatingButtonPositon'"
      >
        <el-radio v-for="o in oItem.options" :key="o.id" :label="o.value">{{
          o.name
        }}</el-radio>
      </el-radio-group>

      <div class="offset"></div>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
import Bus from "@/utils/bus";
export default {
  name: "VlonFloatButton",
  data() {
    return {
      value: null, //二维码图片地址
      oItem: null,
      //为了防止页面初始化时，触发不必要的通知更新，此处设置标识符，初始化后2秒置为true
      needUpdate: false,
    };
  },
  created() {},
  props: ["item", "rightIndex", "catalogIndex", "id"],
  components: {
    chooseLinkComponent: () => import("./chooseLinkComponent.vue"),
    chooseImageComponent: () => import("./chooseImageComponent.vue"),
  },
  mounted() {
    this.$nextTick(() => {
      this.init();
    });
    setTimeout(() => {
      this.needUpdate = true;
    }, 1000);
    Bus.$off(`floatBtnEventIsqrcodeAfter${this.id}`);
    Bus.$on(`floatBtnEventIsqrcodeAfter${this.id}`, (params) => {
      this.updateQrcodeToParent(params.url);
    });
  },
  methods: {
    init() {
      if (this.item) {
        this.oItem = null;
        this.$nextTick(() => {
          this.oItem = JSON.parse(JSON.stringify(this.item));
          if (this.oItem.value === "qrcode") {
            this.value = this.oItem.options[1].qrcode.value;
          }
        });
      }
    },
    //选择悬浮按钮的点击事件,是跳转链接 or 显示二维码
    floatBtnEventChange() {
      if (this.needUpdate) {
        let needUpdateMainChild = false;
        // if (this.item.ename === "pageCover") {
        //     needUpdateMain = false;
        // }
        let row = {
          value: this.oItem.value,
          ename: this.item.ename,
          rightIndex: this.rightIndex,
          catalogIndex: this.catalogIndex,
          needUpdateMainChild: needUpdateMainChild,
            needUpdateMain: false,
        };
        this.$emit("updateParent", JSON.stringify(row));
      }
    },
    //选择完链接回调事件
    updateParent(param) {
      let params = JSON.parse(param);
      if (params.value && params.value.indexOf("__vlon__") > -1) {
        let p = params.value.split("__vlon__");
        this.oItem.options[0].link.name = p.name;
        this.oItem.options[0].link.url = p.url;
        if (this.needUpdate) {
          let needUpdateMainChild = false;
          // if (this.item.ename === "pageCover") {
          //     needUpdateMain = false;
          // }
          let row = {
            value: p,
            ename: "floatBtnEventIslink",
            rightIndex: this.rightIndex,
            catalogIndex: this.catalogIndex,
            needUpdateMainChild: needUpdateMainChild,
            needUpdateMain: false,
          };
          this.$emit("updateParent", JSON.stringify(row));
        }
      }
    },
    //选择偏移方位
    floatButtonPositionChange() {
      if (this.needUpdate) {
        let needUpdateMain = false;
        // if (this.item.ename === "pageCover") {
        //     needUpdateMain = false;
        // }
        let row = {
          value: this.oItem.value,
          ename: "floatingButtonPositon",
          rightIndex: this.rightIndex,
          catalogIndex: this.catalogIndex,
          needUpdateMain: needUpdateMain,
        };
        this.$emit("updateParent", JSON.stringify(row));
      }
    },
    //添加二维码图片
    openChooseImg() {
      Bus.$emit("openChooseImgGroup", {
        ename: "floatBtnEventIsqrcode",
        controlid: this.id,
      });
    },
    //替换图片
    updateChooseImg() {
      Bus.$emit("openChooseImgGroup", {
        ename: "floatBtnEventIsqrcode",
        url: this.value,
        controlid: this.id,
      });
    },
    //删除图片
    delImg() {
      this.updateQrcodeToParent("");
    },
    //update qrcode to parent
    updateQrcodeToParent(value) {
      this.value = value;
      this.oItem.options[1].qrcode.url = value;
      if (this.needUpdate) {
        let needUpdateMainChild = false;
        let row = {
          value: value,
          ename: "floatBtnEventIsqrcode",
          rightIndex: this.rightIndex,
          catalogIndex: this.catalogIndex,
          needUpdateMainChild: needUpdateMainChild,
          needUpdateMain: false,
        };
        this.$emit("updateParent", JSON.stringify(row));
      }
    },
  },
  computed: mapState(["decorate"]),
  watch: {},
};
</script>
<style scoped lang="scss">
.vlonFloatButton {
  display: flex;
  box-sizing: border-box;
  .title {
    width: 100px;
    display: flex;
    height: 16px;
    align-items: center;
    justify-content: flex-end;
    padding-right: 10px;
    font-size: 12px;
    box-sizing: border-box;
  }
  .right {
    width: calc(100% - 100px);
    // align-items: center;
    // display: flex;
    .el-radio-group {
      margin-bottom: 15px;
    }
    .chooseLink {
      width: 100%;
      padding-right: 15px;
      box-sizing: border-box;
      height: 100px;
      display: flex;
      align-items: center;
      background-color: #f4f6f8;
      .vlonChooseSingleLink {
        width: 100%;
      }
    }
    .chooseImage {
      width: 100%;
      padding-right: 15px;
      box-sizing: border-box;
      height: 100px;
      display: flex;
      align-items: center;
      background-color: #f4f6f8;
      .chooseImageComponent {
        width: 100%;
        display: flex;
        align-items: center;
        .title {
          width: 100px;
          min-height: 32px;
          display: flex;
          justify-content: flex-end;
          font-size: 12px;
          align-items: center;
          padding-right: 10px;
        }
        .right {
          display: flex;
          align-items: center;
          flex: 1;
          width: calc(100% - 100px);
          .buttonWrapper {
            position: relative;
            display: flex;
            align-items: center;
            height: 40px;
            background-color: #fff;
            width: 40px;
            .content {
              display: flex;
              justify-content: center;
              align-items: center;
              width: 100%;
              height: 100%;
            }
            .uploadBtn {
              width: 100%;
              height: 100%;
              display: flex;
              box-sizing: border-box;
              border-radius: 2px;
              justify-content: center;
              align-items: center;
              background-color: #fff;
              flex-direction: column;
              position: relative;
              cursor: pointer;
              border: 1px solid #e9edef;
              i {
                color: #0079fe;
                font-weight: bold;
                font-size: 14px;
                margin-top: 5px;
                margin-bottom: 5px;
              }
            }
            img {
              width: 100%;
              height: 100%;
              display: flex;
            }
            .repaceImg,
            .delImg {
              position: absolute;
              z-index: 3;
              opacity: 0.5;
              display: none;
              cursor: pointer;
              &:hover {
                cursor: pointer;
              }
            }
            .repaceImg {
              left: 0;
              bottom: 0px;
              width: 100%;
              height: 20px;
              font-size: 12px;
              background-color: rgba(0, 0, 0, 0.8);
              color: #fff;
              justify-content: center;
            }
            .delImg {
              right: -4px;
              top: -4px;
            }
            &:hover {
              .repaceImg,
              .delImg {
                display: flex;
              }
            }
          }
        }
      }
    }
  }
}
</style>